package com.cash.app.custom

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.view.Window
import androidx.core.graphics.drawable.toDrawable
import com.cash.app.R

class CommonLoading private constructor(
    private val context: Context, private val builder: Builder
) {
    private var dialog: Dialog? = null

    init {
        initDialog()
    }

    private fun initDialog() {
        dialog = Dialog(context, R.style.DialogTheme).apply {
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
            setCancelable(builder.cancelable)
            setCanceledOnTouchOutside(builder.cancelableOnTouchOutside)
            setContentView(R.layout.dialog_loading)
        }
    }

    fun show() {
        dialog?.show()
    }

    fun hide() {
        dialog?.hide()
    }

    fun dismiss() {
        dialog?.dismiss()
    }

    fun isShowing(): Boolean {
        return dialog?.isShowing ?: false
    }

    class Builder(private val context: Context) {
        var cancelable: Boolean = false
        var cancelableOnTouchOutside: Boolean = false

        fun setCancelable(cancelable: Boolean) = apply {
            this.cancelable = cancelable
        }

        fun setCancelableOnTouchOutside(cancelable: Boolean) = apply {
            this.cancelableOnTouchOutside = cancelable
        }

        fun build(): CommonLoading {
            return CommonLoading(context, this)
        }
    }
} 